/**
 * Copyright (C), 2018-2020, 龙腾出行
 * FileName: CorsFilter
 * Author:   zhouw
 * Date:     2020/9/10 11:47
 * Description: 过滤器设置跨域支持
 * History:
 * <author>          <time>          <version>          <desc>
 * zhouw           11:47                       描述
 */
package cn.com.dragonpass.psystem.common.filter;

import cn.com.dragonpass.common.utils.StringUtils;
import org.apache.catalina.connector.ClientAbortException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 过滤器设置跨域支持
 *
 * @author zhouw
 * @create 2020/9/10
 * @since 1.0.0
 */
@Order(1)
@WebFilter(filterName = "corsFilter", urlPatterns = {"/*"})
public class CorsFilter implements Filter {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化filter==========================");
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        response.addHeader("Access-Control-Allow-Origin", "*");
        if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
            try {
                String Origin = request.getHeader("Origin");
                if (StringUtils.isBlank(Origin)) {
                    Origin = "*";
                }
                response.addHeader("Access-Control-Allow-Methods", "*");
                response.addHeader("Access-Control-Allow-Headers", "Content-Type,Token,Code,Credentials,X-Requested-With");
                response.addHeader("Access-Control-Allow-Origin", Origin);
                response.addHeader("Access-Control-Allow-Credentials", "true");
                response.addHeader("Access-Control-Max-Age", "86400");
                response.setStatus(HttpStatus.OK.value());
                PrintWriter writer = response.getWriter();
                writer.flush();
                return;
            } catch (Exception e) {
                e.printStackTrace();
                logger.error(e.getMessage());
                response.setStatus(HttpStatus.METHOD_NOT_ALLOWED.value());
            }
        }
        try {
            filterChain.doFilter(servletRequest, servletResponse);
        } catch (ClientAbortException e) {
            logger.error(e.getMessage());
        }
    }
    @Override
    public void destroy() {
        System.out.println("销毁filter==========================");
    }
}